<?php

/**
 * 仓库设置管理ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */
quanli_require('module/base/wmsHandler.class.php');

class ajax_storeShelfManage {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 13: //货位管理列表
                    $this->shelfManage();
                    break;

                case 14: //解除库位绑定
                    $this->cancel_shelfBind();
                    break;

            }

        }

    }

    //货位管理列表
    function shelfManage(){

        global $G_PGLOB;

        //仓库类
        $wmsHandler = new wmsHandler($G_PGLOB->DBCA);

        if(isset($_REQUEST['storeID']) && $_REQUEST['storeID'] == -1 ){
            unset($_REQUEST['storeID']);
        }

        try {

            //查询
            $result = $wmsHandler->searchShelfGoods($_REQUEST);

            //组织返回的数据
            $result_array = array(
                'page'  => $_REQUEST['page'],
                'total' => $_REQUEST['count'],
                'rows'  => array()
            );

            if( $result ){

                foreach ( $result as $value){

                    $result_array['rows'][] = array(
                        'id' => $value['storeID'].'-'.$value['shelfID'].'-'.$value['postID'],
                        'cell' => array(
                            $value['storeName'],
                            $value['shelf_name'],
                            $value['product_code'],
                            $value['goods_name']
                        )
                    );

                }

            }

            echo json_encode($result_array);
            return;

        }catch(Exception $e){
            error_log($e->getMessage());
            echo json_encode( array('message'=>'系统错误，请与管理员联系','code'=>$e->getCode() ) );
            return;
        }

    }

    //解除货位绑定
    function cancel_shelfBind(){

        global $G_PGLOB;

        //实例化公共类
        $commHandler = new commHandler( $G_PGLOB->DBCA );

        /**接收参数
         *
         * $_REQUEST[delArr] => Array(
        [0] => 12-19-864
        [1] => 18-20-765
        [2] => 17-27-876
        )
         *这是由“仓库号-货位号-商品网购号”组合成的字符串
         */
        $delArr = $_REQUEST['delArr'];

        //删除该仓库的库位信息
        $del_store_shelf_goods = 'DELETE FROM wms_store_shelf_goods WHERE storeID = ? AND shelfID = ? AND postID = ?';

        //统计不存在的货位绑定数目
        $errorNum_store = 0;
        $errorArr = array();

        if( $delArr ){

            //检查要删除的仓库是否全部存在
            foreach( $delArr as $value ){

                list($storeID , $shelfID , $postID ) = explode('-',$value);

                //信息是否存在
                $checkArr = array('storeID'=>$storeID,'shelfID'=>$shelfID,'postID'=>$postID);
                $result   = $commHandler->search($checkArr,'wms_store_shelf_goods');

                if( !$result ){
                    $errorArr[] = $value;
                    $errorNum_store++;
                }

            }

            if( $errorNum_store > 0 ){

                echo json_encode( array('message'=>'所选部分货位绑定已不存在，请刷新页面' , 'code'=>400 ) );
                return;

            }else{

                try{

                    //开启事务
                    $G_PGLOB->DBCA->beginTransaction();

                    //循环删除
                    foreach( $delArr as $value ){

                        list($storeID , $shelfID , $postID ) = explode('-',$value);

                        //解绑货位
                        $G_PGLOB->DBCA->stmt_query($del_store_shelf_goods , 'iii' , array($storeID,$shelfID,$postID) );

                    }

                    //提交事务
                    $G_PGLOB->DBCA->commit();

                    echo json_encode( array('message'=>'解绑成功', 'code'=>200) );
                    return;

                }catch( Exception $e ){

                    $G_PGLOB->DBCA->rollBack();
                    error_log($e->getMessage());
                    echo json_encode( array('message'=>'系统错误，请与管理员联系', 'code'=>500) );
                    return;

                }

            }

        }else{

            echo json_encode( array('message'=>'请选择要解绑的货位' , 'code'=>400 ) );
            return;

        }

    }


}